package cn.imvc.app.models;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Menu;
import cn.imvc.app.entities.Role;
import cn.imvc.app.repositories.MenuRepository;
import cn.imvc.app.repositories.RoleRepository;
import cn.imvc.core.HttpContext;

@Service
public class RoleMenuModelImpl implements RoleMenuModel {
	@Autowired
	private RoleRepository roleRepository;
	@Autowired MenuRepository menuRepository;
	public String getRoles() {
		List<Role> roleList=roleRepository.findAll(new Sort(Direction.DESC,"order"));
		StringBuffer roles=new StringBuffer();
		for (Role role : roleList) {
			roles.append("<li><a href='"+HttpContext.getRootUrl()+"/system/role-menu/"+role.getId()+"' target='ajax' rel='menuTree'>"+role.getName()+"</a></li>");
		}
		return roles.toString();
	}
	
	public String getMenuTree(int roleId) {
		List<Menu> menus = menuRepository.findByParentIsNull(new Sort(Direction.DESC,"order"));
		StringBuffer tree = new StringBuffer();
		String checked="";
		Role role=roleRepository.findOne(roleId);
		for (Menu menu : menus) {
			if(menu.getRole().contains(role)){
				checked="checked='true'";
			}else{
				checked="";
			}
			tree.append("<li><a tname='menuid' tvalue='"+menu.getId()+"' "+checked+" >"+menu.getName()+"</a>");
			tree.append(getChildMenu(menu,role));
			tree.append("</li>");

		}
		return tree.toString();
		
	}
	private String getChildMenu(Menu menu,Role role) {
		StringBuffer childNode = new StringBuffer();

		if (menu.getMenu().size() > 0) {
			childNode.append("<ul>");
			String checked="";
			for (Object object : menu.getMenu()) {
				Menu childMenu = (Menu) object;
				if(childMenu.getRole().contains(role)){
					checked="checked='true'";
				}else{
					checked="";
				}
				childNode.append("<li><a tname='menuid' tvalue='"+childMenu.getId()+"' "+checked+">"+childMenu.getName()+"</a>");
				childNode.append(getChildMenu(childMenu,role));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}

		return childNode.toString();
		
	}

	public Role getRole(int id) {
		return roleRepository.findOne(id);
	}

	public void saveRoleMenu(int id,Set<Menu> menus) {
		Role role=getRole(id);
		for (Object object :role.getMenu()) {
			Menu menu=(Menu)object;
			menu.getRole().remove(role);
		}
		role.getMenu().clear();
		roleRepository.saveAndFlush(role);
		
		role.getMenu().addAll(menus);
		for (Object object : role.getMenu()) {
			Menu menu=(Menu)object;
			menu.getRole().add(role);
		}
		roleRepository.saveAndFlush(role);
	}

	public String getMenus() {
		List<Menu> menus = menuRepository.findByParentIsNull(new Sort(Direction.DESC,"order"));
		StringBuffer menuTree=new StringBuffer();
		for (Menu menu : menus) {
			menuTree.append("<li><a href='"+HttpContext.getRootUrl()+"/system/menu-role/"+menu.getId()+"' target='ajax' rel='roleTree'>"+menu.getName()+"</a>");
			menuTree.append(getChildMenu(menu));
			menuTree.append("</li>");
		}
		return menuTree.toString();
	}
	
	private String getChildMenu(Menu menu){
		StringBuffer childNode = new StringBuffer();
		if (menu.getMenu().size() > 0) {
			childNode.append("<ul>");
			for (Object object : menu.getMenu()) {
				Menu childMenu = (Menu) object;
				childNode.append("<li><a href='"+HttpContext.getRootUrl()+"/system/menu-role/"+childMenu.getId()+"' target='ajax' rel='roleTree'>"+childMenu.getName()+"</a>");
				childNode.append(getChildMenu(childMenu));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}
		
		return childNode.toString();
	}
	
	
	

	public String getRoleTree(int menuid) {
		List<Role> roles = roleRepository.findAll(new Sort(Direction.DESC,"order"));
		StringBuffer tree = new StringBuffer();
		String checked="";
		Menu menu=menuRepository.findOne(menuid);
		for (Role role : roles) {
			if(role.getMenu().contains(menu)){
				checked="checked='true'";
			}else{
				checked="";
			}
			tree.append("<li><a tname='roleid' tvalue='"+role.getId()+"' "+checked+" >"+role.getName()+"</a></li>");
		}
		return tree.toString();
		
	}

	public void saveMenuRole(int id, Set<Role> roles) {
		Menu menu=menuRepository.findOne(id);
		for (Object object :menu.getRole()) {
			Role role=(Role)object;
			role.getMenu().remove(menu);
		}
		menu.getRole().clear();
		menuRepository.saveAndFlush(menu);
		
		menu.getRole().addAll(roles);
		for (Object obj : menu.getRole()) {
			Role role=(Role)obj;
			role.getMenu().add(menu);
		}
		menuRepository.saveAndFlush(menu);
		
	}

}
